package cc.zy.base.hr.service.impl;

import cc.zy.base.common.entity.FebsConstant;
import cc.zy.base.common.entity.QueryRequest;
import cc.zy.base.common.utils.SortUtil;
import cc.zy.base.hr.entity.StaffLabel;
import cc.zy.base.hr.mapper.StaffLabelMapper;
import cc.zy.base.hr.service.IStaffLabelService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author ljy
 * @time 2022/03/05 12:30:03
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class StaffLabelServiceImpl extends ServiceImpl<StaffLabelMapper, StaffLabel> implements IStaffLabelService {

    @Autowired
    private final StaffLabelMapper staffLabelMapper;

    @Override
    public IPage<StaffLabel> getStaffLabels(QueryRequest request, StaffLabel staffLabel) {
        Page<StaffLabel> page = new Page<>(request.getPageNum(), request.getPageSize());
        page.setSearchCount(false);
        page.setTotal(baseMapper.countStaffLabelDetail(staffLabel));
        SortUtil.handlePageSort(request, page, "id", FebsConstant.ORDER_ASC, false);
        return baseMapper.getStaffLabelDetailPage(page, staffLabel);
    }

    @Override
    public StaffLabel getStaffLabelDetailList(Integer id) {
        StaffLabel param = new StaffLabel();
        param.setId(id);
        List<StaffLabel> staffLabel = baseMapper.getStaffLabelDetail(param);
        return CollectionUtils.isNotEmpty(staffLabel) ? staffLabel.get(0) : null;
    }

    @Override
    public StaffLabel getById(Integer id) {
        return baseMapper.getById(id);
    }

    @Override
    public List<StaffLabel> getStaffLabels(StaffLabel staffLabel) {
        LambdaQueryWrapper<StaffLabel> queryWrapper = new LambdaQueryWrapper<>();

        return baseMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createStaffLabel(StaffLabel staffLabel) {
        staffLabel.setCreateDate(new Date());
        staffLabel.setLastModifyDate(new Date());
        this.save(staffLabel);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateStaffLabel(StaffLabel staffLabel) {
        this.saveOrUpdate(staffLabel);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteStaffLabel(StaffLabel staffLabel) {
        LambdaQueryWrapper<StaffLabel> wrapper = new LambdaQueryWrapper<>();
        this.remove(wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteStaffLabels(String[] staffLabelIds) {
        List<String> list = Arrays.asList(staffLabelIds);
        // 删除带有此标签的用户

        // 删除标签
        this.removeByIds(list);
    }

    @Override
    public List<StaffLabel> getStaffLabelListNotPage(StaffLabel staffLabel) {
        List<StaffLabel> staffLabelListNotPage = baseMapper.getStaffLabelListNotPage(staffLabel);
        return staffLabelListNotPage;
    }

    @Override
    public Boolean isRepetitive(String staffLabelName) {
        Integer id = staffLabelMapper.getByName(staffLabelName);
        if (id != null) {
            return false;
        }
        return true;
    }

    @Override
    public Integer getMaxId() {
        return staffLabelMapper.getMaxId();
    }

}
